package com.qf.agro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.common.Constant;
import com.qf.agro.common.CustomException;
import com.qf.agro.common.ResultInfo;
import com.qf.agro.common.SysUserHolder;
import com.qf.agro.mapper.SysRoleMapper;
import com.qf.agro.pojo.SysRole;
import com.qf.agro.service.SysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 10202
 * @version V1.0
 * @Project smart-agriculture-parent
 * @Package com.qf.agro.service.impl
 * @Date 2023/5/23 11:42
 */
@Service
@Transactional(rollbackFor = CustomException.class)
public class SysRoleServiceImpl implements SysRoleService {

   private final SysRoleMapper sysRoleMapper;

   private final RedisTemplate<Object,Object> redisTemplate;

    public SysRoleServiceImpl(SysRoleMapper sysRoleMapper, RedisTemplate<Object, Object> redisTemplate) {
        this.sysRoleMapper = sysRoleMapper;
        this.redisTemplate = redisTemplate;
    }

    @Override
    public ResultInfo getRoleList() {

        List<SysRole> sysRoles = sysRoleMapper.selectList(null);
        return ResultInfo.success(sysRoles);
    }

    @Override
    public ResultInfo selectRoleIdByUserId(Long userId) {

        List<Long> roleIdList=sysRoleMapper.selectRoleIdByUserId(userId);
        return ResultInfo.success(roleIdList);
    }

    @Override
    public ResultInfo getPage(Long page, Long pageSize, String name) {
        Page<SysRole> pages=new Page<>(page,pageSize);
        LambdaQueryWrapper<SysRole> lqw=new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name),SysRole::getName,name);
        pages= sysRoleMapper.selectPage(pages, lqw);
        return ResultInfo.success(pages);
    }

    @Override
    public ResultInfo updateRole(String userId, List<String> roleIds) {

        ResultInfo resultInfo1 = this.deleteRoleByUserId(userId);

        ResultInfo resultInfo2 = this.insertRoleByUserId(userId, roleIds);

        Integer code1 = resultInfo1.getCode();
        Integer code2 = resultInfo2.getCode();

        if(code1==1&&code2==1){
            redisTemplate.delete(Constant.PERMISSION_LIST_USER_ID_PREFIX+ SysUserHolder.get().getId());
        }

        return code1==1&&code2==1?ResultInfo.success("更新成功"):ResultInfo.error("更新失败");
    }

    @Override
    public ResultInfo deleteRoleByUserId(String userId) {
        Boolean success=sysRoleMapper.deleteRoleByUserId(userId);
        return success?ResultInfo.success("删除成功"):ResultInfo.error("删除失败");
    }

    @Override
    public ResultInfo insertRoleByUserId(String userId, List<String> roleIds) {

        boolean success=true;
        if(roleIds!=null){
            for (String roleId : roleIds) {
                if(!sysRoleMapper.insertRoleByUserId(userId,roleId)){
                    success=false;
                    break;
                }
            }
        }
        return success?ResultInfo.success("插入成功"):ResultInfo.error("插入失败");
    }

    @Override
    public ResultInfo insertRole(SysRole sysRole) {

        sysRoleMapper.insert(sysRole);
        return ResultInfo.success("添加成功");
    }

    @Override
    public ResultInfo updateRoleMessage(SysRole sysRole) {
        sysRoleMapper.updateById(sysRole);
        return ResultInfo.success("修改成功");
    }

    @Override
    public ResultInfo deleteRoleById(String id) {

        LambdaQueryWrapper<SysRole> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SysRole::getId,id);
         sysRoleMapper.delete(lqw);
        return ResultInfo.success("删除成功");
    }
}
